package com.example.luke.cashorderingsystem.Util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * Created by yrdev on 7/18/16.
 */
public class GlobalFunction {
    public static ProgressDialog dialog;
    private static final String lanKey="lan_key";
    public static Context context;

    public static void printText(String tag, String content){
        Log.d(tag,content);

    }

    public static void printTextByTag(String content){
        Log.d("tags",content);

    }
    public static String getString(Context context,int id){
        return getResourceString(context,id);

    }

    public static void showProgress(Context context, String title, String body){
        if (GlobalFunction.dialog==null){
            GlobalFunction.dialog= ProgressDialog.show(context,title,body);

        }else {
            GlobalFunction.dialog.show();
        }
    }
    public static void  dismissProgress(){
        if (GlobalFunction.dialog!=null){
            GlobalFunction.dialog.dismiss();
            GlobalFunction.dialog=null;
            
        }
    }

    public static String getSerialNumber(){

        String serial = null;
         String android_id =  Settings.Secure.getString(context.getContentResolver(),
                 Settings.Secure.ANDROID_ID);



        return android_id.toUpperCase();


    }

    public static   void initLanguage(Context context){

        Locale myLocale= new Locale(GlobalFunction.getCurrentLanKey(context));
        Resources res=context.getResources();
        DisplayMetrics dm=res.getDisplayMetrics();
        Configuration configuration=res.getConfiguration();
        configuration.locale=myLocale;
        res.updateConfiguration(configuration,dm);


    }
    public static String getResourceString(Context context, int key){
        return context.getResources().getString(key);

    }


    public   static  String convertToDecimal1(String hex){
        String value="";

        try {

            int i=0;
            ArrayList<String>arrayList=new ArrayList<>();

            //  int temp=0;
            while (i<hex.length()){

                String str=hex.substring(i, i + 2);
                // GlobalFunction.printText("tag","a:"+str);
                arrayList.add(0,str);

                i=i+2;

            }
            String temp="";
            for (int j=arrayList.size()-1;j>=0;j--){

                temp=temp+arrayList.get(j);
            }

            value=String.valueOf(Integer.parseInt(temp, 16 ));

        }
        catch (Exception e){

        }
        //   GlobalFunction.printText("tag",value);
        return value;


    }

    public static String getCurrentLanKey(Context context){

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

        return settings.getString(GlobalFunction.lanKey, Locale.getDefault().getLanguage());

    }

    public static void setLanKey(Context context, String key){

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

        SharedPreferences.Editor editor = settings.edit();
        editor.putString(GlobalFunction.lanKey,key ).apply();

    }


    public static void showKeyboardForEditText(Context context, EditText editText){
        InputMethodManager imm = (InputMethodManager)context. getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
    public static int convertToInt(String value){
        return Integer.valueOf(value);

    }
    public static void ToastAlert(Context context, String alert){
        Toast.makeText(context,alert, Toast.LENGTH_SHORT).show();
    }

    public  static  int converToPx(Context context, int dp){
    Resources r = context.getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    return (int)px;

    }

    public  static String getCurrentDateTime(){

            DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
            Date date = new Date();
            return dateFormat.format(date);

    }

    public  static String getStringFromDate(Date date,String format){

        DateFormat dateFormat = new SimpleDateFormat(format, Locale.getDefault());

        return dateFormat.format(date);

    }
    public  static String httpLatestReqTime(){

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);

    }


    public  static String getCurrentDate(){

        DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);

    }

    public  static String getCurrentTime(){

        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);

    }

    public static String getOnlyID(Context context){

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String ts = String.valueOf( System.currentTimeMillis());


        return ts+tm.getDeviceId().substring(0,4);


    }
 public static String convertToHexString(int x){

        System.out.println(x + "的十六进制结果是：" + Integer.toHexString(x));
        String hexString= Integer.toHexString(x);
        if (hexString.length()==1){
            hexString="0"+hexString;

        }
        return hexString.toUpperCase();

    }

//    public static ArrayList<String> getConvertedName(String name){
//
//        ArrayList<String> arrayList=new ArrayList<>();
//        if (!arrayList.contains(name)){
//
//            arrayList.add(name);
//        }
//
//        char[] tempArr=name.toCharArray();
//
//        for (char splitted:tempArr){
//
//            String[]converted= PinyinHelper.toHanyuPinyinStringArray(splitted);
//
//            if (converted!=null && converted[0].length()>0){
//
//                if (!arrayList.contains(converted[0])){
//                    arrayList.add(converted[0]);
//                }
//            }
//        }
//
//
//
//        return arrayList;
//
//    }

}
